import express from 'express';
import asyncHandler from 'express-async-handler';
import logger from '../../common/logger';
import { ModelParseParamRequest, ModelTreesRequest } from '../../domain/service/simModel/simModelRequest';
import { parameterService } from '../../domain/service/simModel/simModelService';

export function init(app: express.Application) {

  // 解析仿真模型参数
  app.post('/api/model/parse/param', asyncHandler((async (req, res) => {
    const modelParseParamReq = req.body as ModelParseParamRequest;
    logger.info(`parse model param, request params = ${JSON.stringify(modelParseParamReq)}`);
    if (!modelParseParamReq.modelPath) {
      res.json({
        success: false,
        message: 'model path is required'
      });
      return;
    }
    if (modelParseParamReq.type === 'local' && !modelParseParamReq.serverIdentity) {
      res.json({
        success: false,
        message: '模型文件指定在仿真服务器时，需要指定具体的仿真服务器'
      });
      return;
    }
    const ret = await parameterService.parseModelParam(modelParseParamReq);
    res.json({
      success: ret.success,
      message: ret.message,
      data: ret.data
    });
  })));

  // 获取模型文件树
  app.post('/api/model/file/trees', asyncHandler((async (req, res) => {
    const modelTreesRequest = req.body as ModelTreesRequest;
    logger.info(`get model file trees, request params = ${JSON.stringify(modelTreesRequest)}`);
    if (!modelTreesRequest.serverIdentity || !modelTreesRequest.modelPath) {
      res.json({
        success: false,
        message: 'sim server and model path is required'
      });
      return;
    }
    const ret = await parameterService.getModelTrees(modelTreesRequest);
    res.json({
      success: ret.success,
      message: ret.message,
      data: ret.data
    });
  })));

}